package day01;

/**
 * @author feng
 * @Date 2021/10/13 10:29
 */
public class ThreadTest02 implements Runnable {
    private int ticket = 100;
    private Object obj = new Object();
    @Override
    public void run() {
           while (true) {
               synchronized (obj) {
               if (ticket > 0) {
                   try {
                       Thread.sleep(500);
                   } catch (InterruptedException e) {
                       e.printStackTrace();
                   }
                   System.out.println(Thread.currentThread().getName() + "出售" + ticket);
                   ticket--;
               } else {
                   break;
               }

           }
       }
    }

    public static void main(String[] args) {
        ThreadTest02 test02 = new ThreadTest02();
        Thread t2 = new Thread(test02);
        Thread t1 = new Thread(test02);
        Thread t3 = new Thread(test02);
        t1.setName("抽口1");
        t2.setName("抽口2");
        t3.setName("抽口3");

        t2.start();
        t3.start();
        t1.start();

    }
}
